<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Vattu_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */ 
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		
	}	
	
	// Nhập vật tư
	public function add_dinhmuc_to($results)
	{
		$this->load->helper('date');
		//echo date('Y m d'); exit();
		$fields = array(
				'user_id'		=> $this->user->id,
				'name'			=> $this->input->post('tendinhmuc'),
				'created_date 	'	=> date('Y-m-d'),
				'submited_date'	=> date('Y-m-d'),
				'status'		=> 0,
		);
	
		$this->db->set($fields);
		$this->db->insert('dinhmuc');
		$dinhmuc_id = $this->db->insert_id();
	
		// Thực hiện add to hoa don
		foreach ($results as $result)
		{
			$soluong = $this->input->post('soluong_'.$result);
			$this->item_dinhmuc($dinhmuc_id, $soluong, $result);
		}
		return $dinhmuc_id;
	
	}
	
	//Lay danh sach cac vat tu theo dinh muc
	public function get_dinhmuc_by_id($id)
	{
		$this->db->select('dinhmuc_item.*, item.name as itemname, item.code');
		$this->db->from('dinhmuc_item','item');
		$this->db->join('item','item.id = dinhmuc_item.item_id');
		$this->db->where('dinhmuc_id',$id);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Sua dinh muc vat tu
	
	public function edit_bill($id)
	{
		
	}
	
	// them chi tiet vat tu trong dinh muc vua tao
	public function item_dinhmuc($dinhmuc_id, $soluong, $result)
	{
		$fields2 = array(
				'item_id'			=> $result,
				'dinhmuc_id'		=> $dinhmuc_id,
				'amount'			=> $soluong
		);
	
		$this->db->set($fields2);
		$this->db->insert('dinhmuc_item');
	}
	
	/**
	 * ADDED BY MANHVT support multi times deposte site
	 * 
	**/
	/*
	 * Lấy định mức và danh sách các item định mức của nó
	 */
	public function get_dinhmuc($id)
	{
		//$id = $this->input->post('dinhmucId');
		$return = null;
		$this->db->select('dinhmuc.*');
		$this->db->from('dinhmuc');
		$this->db->where('id',$id);
		$query = $this->db->get();
		$result = $query->result_array();
		if($result != null)
		{
			// Lấy danh sách các Item
			$this->db->select('item.*,dinhmuc_item.amount');
			$this->db->from('item','dinhmuc_item');
			$this->db->join('dinhmuc_item','dinhmuc_item.item_id=item.id');		
			$this->db->where('dinhmuc_item.dinhmuc_id',$id);
			$query = $this->db->get();
			$arr = $query->result_array();
			$result[0]['items']=$arr;
			return $result[0];
		}
		else {
			return null;
		}
	}
	public function create_dinhmuc()
	{
		$hddstring = '';
		$ids = explode(',',$hddstring);
		// Insert dinh muc
		
		// add item to dinh muc
		foreach($ids as $id)
		{
			//Get giá trị amount của từng sản phẩm
			$o['amount'] = 0;
			$o['item_id'] = $id;
			$o['dinhmuc_id'] = $dinhmuc_id;
			$this->add_item_to_dinhmuc($o);
		}		
	}
	public function add_item_to_dinhmuc($o)
	{
		$fields = array(
				'dinhmuc_id'		=> $o['dinhmuc_id'],
				'item_id'			=> $o['dinhmuc_id'],
				'amount'			=> $o['amount']
		);		
		$this->db->set($fields);
		$this->db->insert('dinhmuc_item');
	}
	public function count_all_dinhmuc($status='')
	{
		$this->db->select('id');
		$this->db->from('dinhmuc');
		//$this->db->where('type',1);
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	public function get_list_dinhmuc($limit,$start,$status='')
	{
		$this->db->select('dinhmuc.*');
		$this->db->from('dinhmuc');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	/* Đếm số phiếu xuất bởi user đăng nhập
	 * 
	 */
	public function count_all_billimport_by($status='')
	{
		$this->db->select('id');
		$this->db->from('nhap_xuat');
		$this->db->where('type',1);
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	
	
	public function count_all_billexport_by($status='')
	{
		$this->db->select('id');
		$this->db->from('nhap_xuat');
		$this->db->where('type',2);
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	/*
	 * Lấy các bản ghi bới được tạo bởi user đăng nhập
	 */
	public function get_list_billimport_by($limit,$start,$status='')
	{
		$this->db->select('nhap_xuat.*');
		$this->db->from('nhap_xuat');		
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('type',1);
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	
	
	public function get_list_billexport_by($limit,$start,$status='')
	{
		$this->db->select('nhap_xuat.*');
		$this->db->from('nhap_xuat');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('type',2);
		$this->db->where('user_id',$this->user->id);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	
	
	/*
	 * Lấy tất cả vật tư
	 */
	public function get_all_vattu()
	{
		
		$this->db->select('item.*, dvt.name as dvt');
		$this->db->from('item');
		$this->db->join('dvt', 'dvt.id = item.id_dvt', 'left');
		$this->db->order_by('id','desc');
		$this->db->where('deleted',0);
		/*$this->db->select('hocvien.*');
		$this->db->from('hocvien');*/
		
		$query = $this->db->get();
		return $query->result_array();
	}
	//submit item to lock (status = 1)
	public function submit($item_id)
	{
		$data = array('status' => 1);
		$this->db->where('id', $item_id);
		$this->db->set($data);
		$this->db->update('item');
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	// Lấy các item đã được duyệt (status =1)
	public function get_all_submit_items($limit,$start)
	{
		$this->db->select('item.*, dvt.name as dvt');
		$this->db->from('item');
		$this->db->join('dvt', 'dvt.id = item.id_dvt', 'left');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('deleted',0);
		$this->db->where('status',1);
		$query = $this->db->get();
		return $query->result_array();
	}	
	// Lây các item nháp (status = 0)
	public function get_all_draf_items($limit,$start)
	{
		$this->db->select('item.*, dvt.name as dvt');
		$this->db->from('item');
		$this->db->join('dvt', 'dvt.id = item.id_dvt', 'left');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('deleted',0);
		$this->db->where('status',0);
		$query = $this->db->get();
		return $query->result_array();
	}
	//Start Controller List vat tu
	public function get_list_vattu($limit, $start)
	{
		$this->db->select('item.*, dvt.name as dvt');
		$this->db->from('item');
		$this->db->join('dvt', 'dvt.id = item.id_dvt', 'left');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$this->db->where('deleted',0);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_item($id)
	{
		$this->db->select('*');
		$this->db->from('item');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function count_all_vattu($status="")
	{
		$this->db->select('id');
		$this->db->from('item');
		$this->db->where('deleted',0);
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	//Start Controller List Vat tu
	
	
	
	function get_dvt_name()
	{
		$this->db->select('*');
		$this->db->from('dvt');
		//$this->db->where('status',1);
		//$this->db->where('deleted',0);
		$this->db->order_by('id','ASC');		
		$query = $this->db->get();
		return $query->result();
	}
	
	// Thêm mới Vật tư
	public function add_vattu()
	{
		$fields = array(
			'name'			=> $this->input->post('tenvt'),
			'code'			=> mb_strtoupper($this->input->post('mavt')),
			'mo_ta'			=> $this->input->post('mota'),
			'id_dvt'		=> $this->input->post('dvt'),
			'type'			=> $this->input->post('type'),
		);
		
		$this->db->set($fields);
		$this->db->insert('item');
	}
	
	
	// Sửa thông tin Vật tư
	public function edit_item($id)
	{
		$fields = array(
				//'mahv' 			=> $this->input->post('mahv'),
				'name'			=> $this->input->post('tenvt'),
				'code'			=> mb_strtoupper($this->input->post('mavt')),
				'status'		=> 1,
				'id_dvt'			=> $this->input->post('dvt'),
				'mo_ta'			=> $this->input->post('mota'),
		);
	
	
		$this->db->set($fields);
	
		$this->db->where('id', $id);
		$this->db->update('item');
	}
	
	// Nhập vật tư
	public function add_to($results)
	{
		$this->load->helper('date');
		//echo date('Y m d'); exit();
		$fields = array(
				'user_id'		=> $this->user->id,
				'type'			=> 1,
				'code'			=> $this->input->post('ordercode'),
				'date_time 	'	=> date('Y-m-d'),
				'status'		=> 0,
		);
		
		$this->db->set($fields);
		$this->db->insert('nhap_xuat');
		$nhapxuat_id = $this->db->insert_id();
		
		// Thực hiện add to hoa don
		foreach ($results as $result)
		{
			$soluong = $this->input->post('soluong_'.$result);
			$this->item_detail($nhapxuat_id, $soluong, $result);
		}
		return $nhapxuat_id;
		
	}
	
	// them chi tiet vat tu trong phieu vua tao
	public function item_detail($nhapxuat_id, $soluong, $result)
	{
		$fields2 = array(
				'item_id'			=> $result,
				'xuat_nhap_id'		=> $nhapxuat_id,
				'amount'			=> $soluong
		);
		
		$this->db->set($fields2);
		$this->db->insert('item_xuat_nhap');
	}
	
	public function change_status($id, $status = 0)
	{
		$data = array(
				'status' => $status,
		);
	
		$this->db->where('id', $id);
		$this->db->update('item', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function getLastInserted() {
	    return $this->db->insert_id();
	}
	
	public function check_code()
	{
		$code = $this->input->post('mavt');
        $query =  "
			SELECT id, code
			FROM item WHERE code ='$code' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
}

/* Location: ./application/models/qlsv_model.php */
